package br.mestrado.unb.bs.sensor;

import br.mestrado.unb.bs.communication.IBluetoothWriter;
import br.mestrado.unb.bs.model.configuration.SensorConfiguration;
import br.mestrado.unb.bs.model.configuration.SensorIdentification;
import br.mestrado.unb.bs.model.constant.DataPackageConstant;
import br.mestrado.unb.bs.model.sensor.DataPackageWrapper;
import br.mestrado.unb.bs.model.sensor.Payload;
import br.mestrado.unb.bs.model.sensor.TemperaturePayload;
import br.mestrado.unb.bs.util.BluetoothSenderConstant;

public class TemperatureSensorThread extends ThreadSensor{


	public TemperatureSensorThread(SensorConfiguration config, SensorIdentification id, IBluetoothWriter writer) {
		super(config, id, writer);
		setName(this.getClass().getName());
	}

	@Override
	protected void sendValue() throws Exception {
		
		Payload objPayload = this.config.getConfiguration(id);
		if(objPayload instanceof TemperaturePayload){
			TemperaturePayload tempPayload = (TemperaturePayload) objPayload;

			TemperaturePayload payload = new TemperaturePayload(tempPayload.getLowTemperature(),tempPayload.getHighTemperature());
			payload.setBattery(tempPayload.getBattery());
			payload.setSampleFrequency(tempPayload.getSampleFrequency());
			payload.setCategory(DataPackageConstant.temp_category);
			payload.setHighTemperature(tempPayload.getHighTemperature());
			payload.setLowTemperature(tempPayload.getLowTemperature());
			
			this.period = BluetoothSenderConstant.TEN_SECONDS/(int)payload.getSampleFrequency();
			DataPackageWrapper dataPackage = new DataPackageWrapper(payload);
			bluetooth.write(dataPackage.toByteStream());
			window.addMessage("(TEMP , " + this.id.getSensorId() + ")" + ": Enviou." + payload.toString());
			
		}

		
	}	
}
